package com.kgc.mall.servlet;

import cn.hutool.core.util.StrUtil;
import com.kgc.mall.common.Constants;
import com.kgc.mall.pojo.User;
import com.kgc.mall.service.UserService;
import com.kgc.mall.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.List;

@WebServlet(urlPatterns = {"/backend/users/*"})
public class UserInfoServlet extends BaseServlet {

    private static UserService userService = new UserServiceImpl();

    protected void listPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //前台传数据给后台
        long userCount=0;
        long pageCount=0;
        int pageNum=1;
        int pageSize=5;
        String curPage = request.getParameter("curPage");
        String userName = request.getParameter("userName");
        String loginName = request.getParameter("loginName");
        if (curPage!=null&&!"".equals(curPage)){
            pageNum=Integer.parseInt(curPage);
        }


        List<User> userList =   userService.getUserInfoByPage(loginName,userName,pageNum,pageSize);
        userCount=userService.getUserTotalCount(loginName,userName);
        pageCount=userCount/(long)pageSize;
        request.setAttribute("pageNum",pageNum);
        request.setAttribute("pageCount",pageCount);
        request.setAttribute("userList",userList);
        request.getRequestDispatcher("/userList.jsp").forward(request,response);
        //后台传数据给前台

    }
    protected void  deleteUser(HttpServletRequest request,HttpServletResponse response) throws ServerException,IOException{
        String id=request.getParameter("id");
        userService.deleteUserByUid(Integer.parseInt(id));
        response.sendRedirect("/backend/user?action=listPage");
    }

}
